package com.fysports.web.service.user;

import com.google.common.base.Preconditions;
import com.fysports.web.kit.commons.StringKit;
import com.fysports.web.model.user.BizUser;
import com.fysports.web.model.user.UserAddress;
import com.fysports.web.model.user.UserFavorite;
import com.fysports.web.model.user.UserRank;
import com.fysports.web.service.ServiceException;

import java.util.List;


/**
 * Class title: UserEntity实现 <br/>
 * Describe:  User系统中频繁交互,采用service接口隔离     <br/>
 * Created by konbluesky           <br/>
 * Date : 15/12/28 下午5:26       <br/>
 * Project : weixin    <br/>
 */
public class UserEntityImpl implements UserEntity {
    private String useruuid;
    private BizUser user;
    private UserAddress address;
    public UserEntityImpl(String uuid){
        Preconditions.checkNotNull(uuid,new ServiceException("user uuid出错"));
        this.useruuid=uuid;
    }
    @Override public List<UserAddress> getAddressList() {
        return UserAddress.dao.search(UserAddress.COL_USERUUID,useruuid);
    }


    @Override public UserAddress getAddressDefault() {
        if(address==null){
            address=UserAddress.dao.findFirst("select * from user_address where useruuid=? and isdefault=1",useruuid);
            if(address==null){
                address=UserAddress.dao;
            }
        }
        return address;
    }


    @Override public BizUser getUserInfo() {
        if(user==null) {
            user = BizUser.dao.findById(useruuid);
        }
        return user;
    }


    @Override public List<UserFavorite> getUserFavorite() {
        return UserFavorite.dao.search(UserFavorite.COL_USERUUID,useruuid);
    }


    @Override public UserRank getUserRank() {
        //TODO 获取用户下线个数,通过个数计算用户等级
        //此处暂时使用固定等级
        return UserRank.dao.searchFirst(UserRank.COL_UUID,getUserInfo().getRankuuid());
    }


    @Override public boolean isPass() {
        String idcard=getUserInfo().getIdcard();
        String realname=getUserInfo().getRealname();
        String phone=getUserInfo().getPhone();
        if(StringKit.isBlank(idcard)||
                StringKit.isBlank(realname)||
                StringKit.isBlank(phone)){
            return false;
        }
        return true;
    }
}
